package org.zcl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.zcl.dao.UserTestDao;
import org.zcl.entity.UserTest;
import org.zcl.service.UserTestService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 用户表(UserTest)表服务实现类
 *
 * @author Chenglin Zhu
 * @since 2022-03-15 16:07:41
 */
@Service
public class UserTestServiceImpl extends ServiceImpl<UserTestDao, UserTest> implements UserTestService {

    @Override
    public List<UserTest> selectAsc() {
        List<UserTest> list = this.list(new LambdaQueryWrapper<>());
        Map<Integer, UserTest> map = list.stream().collect(Collectors.toMap(UserTest::getId, Function.identity(), (k1, k2) -> k1));
        List<UserTest> resultList = new ArrayList<>();
        // 找到第一个
        UserTest first = list.stream().filter(u -> u.getPre() == null).findFirst().orElse(null);
        assert first != null;
        resultList.add(first);
        addList(map, resultList, first.getNext());
        return resultList;
    }

    @Override
    public Boolean edit(UserTest newUserTest) {
        UserTest oldUserTest = this.getById(newUserTest.getId());
        Integer newPre = newUserTest.getPre();
        Integer newNext = newUserTest.getNext();
        List<UserTest> list = new ArrayList<>();
        list.add(newUserTest);
        Integer repId = null;
        if (newPre != null) {
            UserTest newPreNode = this.getById(newPre);
            repId = newPreNode.getNext();
            newPreNode.setNext(newUserTest.getId());
            list.add(newPreNode);
        }
        if (newNext != null) {
            UserTest newNextNode = this.getById(newNext);
            if (repId == null) {
                repId = newNextNode.getPre();
            }
            newNextNode.setPre(newUserTest.getId());
            list.add(newNextNode);
        }
        UserTest repNode = this.getById(repId);
        if (repNode != null) {
            Integer repPreId = oldUserTest.getPre();
            if (repPreId != null) {
                UserTest repPreNode = this.getById(repPreId);
                repPreNode.setNext(repNode.getId());
                list.add(repPreNode);
            }
            Integer repNextId = oldUserTest.getNext();
            if (repNextId != null) {
                UserTest repNextNode = this.getById(repNextId);
                repNextNode.setPre(repNode.getId());
                list.add(repNextNode);
            }
            repNode.setPre(repPreId);
            repNode.setNext(repNextId);
            list.add(repNode);
        }
        return this.updateBatchById(list);
    }

    private void addList(Map<Integer, UserTest> map, List<UserTest> resultList, Integer next) {
        if (next == null) {
            return;
        }
        UserTest userTest = map.get(next);
        resultList.add(userTest);
        addList(map, resultList, userTest.getNext());
    }
}

